package cn.edu.xmu.oomall.aftersales.dao;

import cn.edu.xmu.javaee.core.util.CloneFactory;
import cn.edu.xmu.oomall.aftersale.dao.bo.OrderItem;
import cn.edu.xmu.oomall.aftersale.mapper.OrderItemPoMapper;
import cn.edu.xmu.oomall.aftersale.mapper.po.OrderItemPo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
@RefreshScope
public class OrderItemDao {
    private final static Logger logger = LoggerFactory.getLogger(AftersaleDao.class);

    @Value("${oomall.region.timeout}")
    private int timeout;

    private final OrderItemPoMapper orderItemPoMapper;

    public OrderItemDao(OrderItemPoMapper orderItemPoMapper) {
        this.orderItemPoMapper = orderItemPoMapper;
    }

    public OrderItem build(OrderItemPo po){
        return CloneFactory.copy(new OrderItem(), po);
    }

    public OrderItem findById(Long id) {
        Optional<OrderItemPo> ret = orderItemPoMapper.findById(id);
        if (ret.isPresent()) {
            logger.debug("findById: retrieve from database orderitem = {}", ret.get());
            return this.build(ret.get());
        } else {
            throw new BusinessException(ReturnNo.RESOURCE_ID_NOTEXIST, String.format(ReturnNo.RESOURCE_ID_NOTEXIST.getMessage(), "订单项", id));
        }
    }
}
